'-----------------------------------------------------------------------
'  This file is part of the Microsoft .NET SDK Code Samples.
' 
'  Copyright (C) Microsoft Corporation.  All rights reserved.
' 
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation.  See these other
'materials for detailed information regarding Microsoft code samples.
' 
'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'-----------------------------------------------------------------------

Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO

Namespace Microsoft.Samples.WinForms.VB.PrintingExample5

    Public Class PrintForm
        Inherits System.Windows.Forms.Form

        Private storedPageSettings As PageSettings

        Public Sub New ()

            MyBase.New

            PrintForm = Me

            'This call is required by the Windows Forms Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

            'Wire up events for print buttons
            AddHandler printButton.Click, AddressOf printButton_Click
            AddHandler pageSetupButton.Click, AddressOf pageSetupButton_Click
            AddHandler printPreviewButton.Click, AddressOf printPreviewButton_Click

        End Sub

        'Event fired when the user presses the page setup button
        Private Sub pageSetupButton_Click(sender As object, e As System.EventArgs)

            Try
                Dim psDlg As New PageSetupDialog

                If (storedPageSettings Is Nothing) Then
                    storedPageSettings = new PageSettings()
                End If

                psDlg.PageSettings = storedPageSettings
                psDlg.ShowDialog
            Catch ex As Exception
                MessageBox.Show("An error occurred - " + ex.Message)
            End Try

        End Sub


        'Event fired when the user presses the print button
        Private Sub printButton_Click(sender As object, e As System.EventArgs)

            Try
                Dim streamToPrint As StreamReader = new StreamReader ("PrintMe.Txt")
                Try
                    'Assumes the default printer
                    Dim pd As TextFilePrintDocument = new TextFilePrintDocument(streamToPrint)

                    If Not (storedPageSettings Is Nothing) Then
                        pd.DefaultPageSettings = storedPageSettings
                    End If


                    Dim dlg As New PrintDialog()
                    dlg.Document = pd
                    Dim result As DialogResult = dlg.ShowDialog()

                    If (result = System.Windows.Forms.DialogResult.OK) Then
                        pd.Print()
                    End If

                Finally
                    streamToPrint.Close()
                End Try

            Catch ex As Exception
                MessageBox.Show("An error occurred printing the file - " + ex.Message)
            End Try

        End Sub


        'Event fired when the user presses the print preview button
